package com.learning.platform.controller;

import com.learning.platform.dto.ApiResponse;
import com.learning.platform.dto.CourseReviewRequest;
import com.learning.platform.dto.CourseReviewResponse;
import com.learning.platform.security.UserPrincipal;
import com.learning.platform.service.CourseReviewService;
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/courses/{courseId}/reviews")
public class CourseReviewController {
    
    private final CourseReviewService reviewService;
    
    public CourseReviewController(CourseReviewService reviewService) {
        this.reviewService = reviewService;
    }
    
    /**
     * 获取课程评价列表
     */
    @GetMapping
    public ApiResponse<Page<CourseReviewResponse>> getCourseReviews(
            @PathVariable Long courseId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        
        Pageable pageable = PageRequest.of(page - 1, pageSize);
        Page<CourseReviewResponse> reviews = reviewService.getCourseReviews(courseId, pageable);
        return ApiResponse.success(reviews);
    }
    
    /**
     * 提交课程评价
     */
    @PostMapping
    public ApiResponse<CourseReviewResponse> createReview(
            @PathVariable Long courseId,
            @Valid @RequestBody CourseReviewRequest request,
            Authentication auth) {
        
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        CourseReviewResponse response = reviewService.createReview(courseId, request, principal.getUserId());
        return ApiResponse.success(response, "评价提交成功");
    }
    
    /**
     * 更新评价
     */
    @PutMapping("/{reviewId}")
    public ApiResponse<CourseReviewResponse> updateReview(
            @PathVariable Long courseId,
            @PathVariable Long reviewId,
            @Valid @RequestBody CourseReviewRequest request,
            Authentication auth) {
        
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        CourseReviewResponse response = reviewService.updateReview(reviewId, request, principal.getUserId());
        return ApiResponse.success(response, "评价更新成功");
    }
    
    /**
     * 删除评价
     */
    @DeleteMapping("/{reviewId}")
    public ApiResponse<Void> deleteReview(
            @PathVariable Long courseId,
            @PathVariable Long reviewId,
            Authentication auth) {
        
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        reviewService.deleteReview(reviewId, principal.getUserId());
        return ApiResponse.success(null, "评价删除成功");
    }
    
    /**
     * 检查用户是否已评价
     */
    @GetMapping("/check")
    public ApiResponse<Boolean> checkUserReview(
            @PathVariable Long courseId,
            Authentication auth) {
        
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        boolean hasReviewed = reviewService.hasUserReviewed(principal.getUserId(), courseId);
        return ApiResponse.success(hasReviewed);
    }
    
    /**
     * 获取用户对课程的评价
     */
    @GetMapping("/my")
    public ApiResponse<CourseReviewResponse> getUserReview(
            @PathVariable Long courseId,
            Authentication auth) {
        
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        CourseReviewResponse review = reviewService.getUserReview(principal.getUserId(), courseId);
        return ApiResponse.success(review);
    }
}

